package com.workflow.service.impl;

import java.util.List;

import com.base.bean.Cardinfo;
import com.common.bean.Datadict;
import com.common.util.AbstractManager;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.workflow.bean.Inputcard;
import com.workflow.bean.Inputdeptdetail;
import com.workflow.service.InputdeptdetailService;

public class InputdeptdetailServiceImpl extends AbstractManager implements
		InputdeptdetailService {

	public void addInputdeptdetail(Inputdeptdetail inputdeptdetail) {
		String hql = "select count(*) from Inputdeptdetail indd where indd.inputdept.inputno = ?";
		long count = 0;
		count = (Long)this.getHibernateTemplate().find(hql,inputdeptdetail.getInputdept().getInputno()).get(0);
		count %= 1000L;
		
//		Long num = Long.parseLong(DateHelper.dateToStr(new Date(), "yyyyMMddHmsS"));
//		num *= 1000L;		
//		num += count;
		
		String inputno = inputdeptdetail.getInputdept().getInputno();
		
		String intdetailno = "RKD-" + inputno.substring(inputno.length()-9, inputno.length()) + count;
		inputdeptdetail.setIntdetailno(intdetailno);
		this.getHibernateTemplate().save(inputdeptdetail);
	}

	@SuppressWarnings("unchecked")
	public List<Inputdeptdetail> findAllInputdeptdetail() {
		String hql = "from Inputdeptdetail idd order by idd.inputdept.inputno , idd.intdetailno";
		return this.getHibernateTemplate().find(hql);
	}

	public Inputdeptdetail findInputdeptdetail(String intdetailno) {
		return (Inputdeptdetail) this.getHibernateTemplate().load(Inputdeptdetail.class, intdetailno);
	}

	public PagerModel findInputdeptdetailForPager() {
		String hql = "from Inputdeptdetail idd order by idd.inputdept.inputno , idd.intdetailno";
		return searchPaginated(hql);
	}
	
	public PagerModel findInputdeptdetailForPager(String inputno) {
		String hql = "from Inputdeptdetail idd where idd.inputdept.inputno = ? order by idd.intdetailno";
		return searchPaginated(hql,inputno);
	}

	@SuppressWarnings("unchecked")
	public void removeInputdeptdetail(String intdetailno) {
		Inputdeptdetail inputdeptdetail = null;
		inputdeptdetail =  (Inputdeptdetail) this.getHibernateTemplate().load(Inputdeptdetail.class, intdetailno);
		if(inputdeptdetail != null) {
			String hql = "from Inputcard card where card.intdetailno = ? order by card.cardno";
			List<Inputcard> inputcardlist = this.getHibernateTemplate().find(hql,intdetailno);
			for(Inputcard inputcard : inputcardlist) {
				Cardinfo cardinfo = null;
				cardinfo = (Cardinfo) this.getHibernateTemplate().load(Cardinfo.class, inputcard.getCardno());
				Datadict datadictByStatus = new Datadict();
//				置卡状态为初始 状态编号 71l
				datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_INIT);
				cardinfo.setDatadictByStatus(datadictByStatus);
				cardinfo.setBelongto("");//取消所属状态，临时
				this.getHibernateTemplate().update(cardinfo);
				this.getHibernateTemplate().delete(inputcard);
			}
			
			this.getHibernateTemplate().delete(inputdeptdetail);
		}
	}

	public void updateInputdeptdetail(Inputdeptdetail inputdeptdetail) {
		this.getHibernateTemplate().update(inputdeptdetail);
	}

}
